package com.jiazhong.boot.rocketmq.test;

import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Component;

@Component
@SpringBootTest
@Slf4j
public class App {
    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @SneakyThrows
    @Test
    public void a() {
        String message = "hello,sync!";
        SendResult sendResult = rocketMQTemplate.syncSend("TopicB:sync", message);
        log.info("sendResult:{}", sendResult);
        Thread.sleep(30000);
    }

    @SneakyThrows
    @Test
    public void b() {
        String message = "hello,async!";
        rocketMQTemplate.asyncSend("TopicB:async", message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                log.info("success:{}", sendResult);
            }

            @Override
            public void onException(Throwable throwable) {
                log.info("error:{}", throwable.getMessage());
            }
        });
        Thread.sleep(30000);
    }

    @Test
    public void c() {
        String message = "hello,oneway!";
        rocketMQTemplate.sendOneWay("TopicB:oneway", message);
    }

    @SneakyThrows
    @Test
    public void d() {
        Message message = new Message("TopicB", "guolv", "Hello RocketMQ!".getBytes());
        message.putUserProperty("age", 18 + "");
        DefaultMQProducer producer = rocketMQTemplate.getProducer();
        producer.send(message);
    }
}
